﻿namespace Xms.Abp.Components;

public class EnumTypeSelect : Select<int, NameValue<int>>
{
    public EnumTypeSelect()
    {
        LabelName = nameof(NameValue<int>.Name);
        ValueName = nameof(NameValue<int>.Value);
    }
    [Parameter] public Type DataSourceType { get; set; }

    [Parameter] public string NullLabel { get; set; } = "请选择";

    protected override async Task OnInitializedAsync()
    {
        var list = new List<NameValue<int>>();
        if (DataSourceType != null)
        {
            var values = Enum.GetValues(DataSourceType);
            foreach (var value in values)
            {
                list.Add(new NameValue<int>(((Enum)value).GetDisplayName(), (int)value));
            }
        }
        if (!NullLabel.IsNullOrWhiteSpace())
        {
            list.Insert(0, new NameValue<int>
            {
                Name = NullLabel,
                Value = 0
            });
        }
        DataSource = list;

        await base.OnInitializedAsync();
    }
}
